<template>
  <div class="body">
    <i class="el-icon-close" @click="closePointEdit"></i>
    <div class="body-top">
      <div class="body-top-title">
        <span>点位编辑</span>
      </div>
    </div>
    <div class="body-content">
      <div class="body-content-top">
        <div class="body-content-top-left"><span>巡视点</span></div>
        <div class="body-content-top-middle"><span>点位结果浏览</span></div>
        <div class="body-content-top-right"><span>任务信息</span></div>
      </div>
      <div class="body-content-bottom">
        <div class="body-content-bottom-left">
          <el-input v-model="keword" placeholder="请输入关键字搜索">
            <i slot="suffix" class="el-input__icon el-icon-search" @click="searchPoints" />
          </el-input>
          <el-checkbox v-model="checkAll" class="filter-checkbox" :indeterminate="isIndeterminate"
            @change="handleCheckAllChange">全选</el-checkbox>
          <el-divider />
          <el-tree ref="tree" class="filter-tree" node-key="areaCode" show-checkbox :data="points" :props="defaultProps"
            :filter-node-method="filterNode" @check-change="handleCheckChange" />
        </div>
        <div class="body-content-bottom-middle">
          <div class="body-content-bottom-middle-table">
            <el-table :data="pointData" style="width: 100%;">
              <el-table-column type="index" label="序号" width="80" />
              <el-table-column prop="parentName" label="设备名称" />
              <el-table-column prop="areaName" label="点位名称" />
              <el-table-column prop="deviceTypeName" label="设备类型" />
              <el-table-column prop="recognitionTypeName" label="识别类型" />
            </el-table>
          </div>
          <el-divider />
          <div class="body-content-bottom-middle-page">
            <el-pagination background layout="total, prev, pager, next" :page-size="pageSize" :total="total"
              :current-page.sync="currentPage" @current-change="handlePage" />
          </div>
        </div>
        <div class="body-content-bottom-right">
          <div class="body-content-bottom-right-info">
            <div class="info">
              <el-input v-model="taskName" placeholder="请输入">
                <span slot="prefix">任务名称：</span>
              </el-input>
            </div>
            <div class="info">
              <el-select v-model="priority" placeholder="请选择" :popper-append-to-body="false">
                <span slot="prefix">优先级：</span>
                <el-option v-for="item in prioritys" :key="item" :label="item" :value="item" />
              </el-select>
            </div>
            <div class="info">
              <el-select v-model="patrolType" placeholder="请选择" :popper-append-to-body="false">
                <span slot="prefix">巡视类型：</span>
                <el-option v-for="item in patrolTypes" :key="item.code" :label="item.name" :value="item.code" />
              </el-select>
            </div>
            <div class="end-info">
              <span>结束动作：</span>
              <el-radio v-model="endAction" label="0">自动充电</el-radio>
              <el-radio v-model="endAction" label="1">原地待命</el-radio>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="footer">
      <el-button type="primary" @click="confirm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  data() {
    return {
      station: null,
      keword: '',
      points: [],
      defaultProps: {
        children: 'children',
        label: 'areaName'
      },
      isIndeterminate: false,
      checkAll: false,
      pointData: [],
      pageSize: 14,
      currentPage: null,
      total: null,
      taskName: '',
      priority: null,
      prioritys: [1, 2, 3, 4, 5],
      patrolType: null,
      patrolTypes: [],
      endAction: '0',
      taskPlanCode: null,
      pointList: [],
      deviceTypes: [],
      recognitionTypes: []
    }
  },
  methods: {
    searchPoints() {
      this.$refs.tree.filter(this.keword)
    },
    filterNode(value, data) {
      if (!value) return true
      return data.areaName.indexOf(value) !== -1
    },
    handleCheckAllChange() {
      this.isIndeterminate = false
      if (this.checkAll) {
        this.checkAll = true
        this.$refs.tree.setCheckedNodes(this.points)
        this.expandAll(true)
      } else {
        this.checkAll = false
        this.$refs.tree.setCheckedNodes([])
        this.expandAll(false)
      }
    },
    expandAll(flag) {
      if (flag) {
        for (var i in this.$refs.tree.store.nodesMap) {
          this.$refs.tree.store.nodesMap[i].expanded = true
        }
      } else {
        for (var j in this.$refs.tree.store.nodesMap) {
          this.$refs.tree.store.nodesMap[j].expanded = false
        }
      }
    },
    handleCheckChange() {
      const checkeds = this.$refs.tree.getCheckedNodes(false, true).length
      const allNodes = Object.keys(this.$refs.tree.store.nodesMap).length
      if (checkeds === allNodes) {
        this.isIndeterminate = false
        this.checkAll = true
      } else {
        this.checkAll = false
        if (checkeds === 0) {
          this.isIndeterminate = false
        } else {
          this.isIndeterminate = true
        }
      }
    },
    handlePage(currentPage) {
      this.getPointInfoList(currentPage)
    },
    closePointEdit() {
      this.$router.push('/szRobot/dashboard/taskEdit')
    },
    confirm() {
      this.rebuildTaskPlan()
    },
    cancel() {
      this.$router.push('/szRobot/dashboard/taskEdit')
    },
    rebuildTaskPlan(){
      axios.post(`http://${this.$store.getters.serverUrl}/station/taskCustomizationControl/rebuildTaskPlan`, {
        endAction: this.endAction,
        patrolType: this.patrolType,
        pointCodes: this.pointList.map(item => item.split('-')[1]).join(','),
        priority:  this.priority,
        taskName: this.taskName,
        taskPlanCode: this.taskPlanCode
      }, {
        params: {
          masterId: this.station
        },
        headers: {
          Gz_security_token: this.$store.getters.Gz_security_token
        }
      }).then(({ data }) => {
        if (data.success) {
          this.$message({
            type: 'success',
            message: '点位编辑成功'
          })
          this.$router.push('/szRobot/dashboard/taskEdit')
        } else {
          this.$message({
            type: 'error',
            message: '点位编辑失败'
          })
        }
      }).catch(() => {
        this.$message({
          type: 'error',
          message: '点位编辑失败'
        })
      })
    },
    getPatrolPoint() {
      axios.post(`http://${this.$store.getters.serverUrl}/station/taskCustomizationControl/getAreaTree`, null, {
        params: {
          masterId: this.station
        },
        headers: {
          Gz_security_token: this.$store.getters.Gz_security_token
        }
      }).then(({ data }) => {
        if (data.success) {
          this.points = data.data
        }
      })
    },
    getTaskType() {
      axios.post(`http://${this.$store.getters.serverUrl}/station/taskCustomizationControl/getTaskType`, null, {
        params: {
          masterId: this.station
        },
        headers: {
          Gz_security_token: this.$store.getters.Gz_security_token
        }
      }).then(({ data }) => {
        if (data.success) {
          if (data.data != null) {
            data.data.forEach(element => {
              if (element.typeName == 'deviceType') {
                this.deviceTypes = element.baseTaskTypeList
              } else if (element.typeName == 'recognitionType') {
                this.recognitionTypes = element.baseTaskTypeList
              } else if (element.typeName == 'patrolType')
                this.patrolTypes = element.baseTaskTypeList
            })
          }
        }
      })
    },
    getPointListByTask() {
      axios.post(`http://${this.$store.getters.serverUrl}/station/taskCustomizationControl/getPointListByTask`, null, {
        params: {
          taskPlanCode: this.taskPlanCode,
          masterId: this.station
        },
        headers: {
          Gz_security_token: this.$store.getters.Gz_security_token
        }
      }).then(({ data }) => {
        if (data.success) {
          this.pointList = data.data
        }
      })
    },
    getPointInfoList(currentPage) {
      axios.post(`http://${this.$store.getters.serverUrl}/station/taskCustomizationControl/getPointInfoList`,
        {
          curPage: currentPage,
          pageSize: this.pageSize,
          devicePointCodeList: this.pointList.map(item => item.split('-')[1])
        },
        {
          headers: {
            Gz_security_token: this.$store.getters.Gz_security_token
          }
        }).then(({ data }) => {
          if (data.success) {
            if (data.data != null) {
              this.pointData = data.data.map(item => {
                for (let i in this.deviceTypes) {
                  if (this.deviceTypes[i].code == item.deviceType) {
                    item.deviceTypeName = this.deviceTypes[i].name
                    break
                  }
                }
                for (let j in this.recognitionTypes) {
                  if (this.recognitionTypes[j].code == item.recognitionType) {
                    item.recognitionTypeName = this.recognitionTypes[j].name
                    break
                  }
                }
                return item
              })
              this.total = data.data.length
            }
          }
        })
    }
  },
  created() {
    this.currentPage = 1
    this.total = 0
  },
  async mounted() {
    this.station = this.$route.query.station
    this.taskPlanCode = this.$route.query.taskPlanCode
    this.taskName = this.$route.query.taskName
    this.priority = this.$route.query.priority
    this.patrolType = this.$route.query.patrolType
    this.endAction = this.$route.query.endAction
    this.getPointListByTask()
    this.getPatrolPoint()
    this.getTaskType()
    setTimeout(() => {
      this.getPointInfoList(this.currentPage)
    }, 1000)
  }
}
</script>
<style scoped>
.body {
  position: relative;
  margin-left: 20px;
  width: 1876px;
  height: 940px;
  background: rgba(1, 55, 66, 1);
  border: 1px solid rgba(0, 188, 191, 1);
  box-shadow: inset 20px 0px 35px -9px rgba(6, 115, 115, 1);

  .el-icon-close {
    position: absolute;
    right: 10px;
    top: 10px;
    background: rgba(22, 189, 202, 1);
    border: 4px solid rgba(22, 189, 202, 1);
    box-shadow: 0px 2px 4px rgba(158, 158, 158, 0.25);
    border-radius: 50%;
    color: rgba(255, 255, 255, 1.00);
    cursor: pointer;
  }
}

.body-top {
  height: 56px;
  background: rgba(80, 211, 225, 0.15);
  display: flex;
  align-items: center;
}

.body-top-title {
  margin-left: 20px;
  font-size: 22px;
  font-weight: 500;
  color: rgba(223, 238, 243, 1);

}

.body-content-top {
  display: flex;
  align-items: center;
  font-size: 20px;
  font-weight: 700;
  color: rgba(0, 255, 255, 1);
}

.body-content-top-left {
  display: flex;
  align-items: center;
  height: 60px;
  width: 350px;
  margin: 16px 21px 15px 18px;
  padding-left: 51px;
  background: linear-gradient(90deg, rgba(0, 255, 255, 0.4) 0%, rgba(13, 128, 236, 0) 100%);
  letter-spacing: 4px;
}

.body-content-top-middle {
  display: flex;
  align-items: center;
  height: 60px;
  width: 1000px;
  margin-top: 16px;
  margin-right: 10px;
  margin-bottom: 15px;
  padding-left: 51px;
  background: linear-gradient(90deg, rgba(0, 255, 255, 0.4) 0%, rgba(13, 128, 236, 0) 100%);
  letter-spacing: 4px;
}

.body-content-top-right {
  display: flex;
  align-items: center;
  height: 60px;
  width: 455px;
  margin-top: 16px;
  margin-bottom: 15px;
  padding-left: 51px;
  background: linear-gradient(90deg, rgba(0, 255, 255, 0.4) 0%, rgba(13, 128, 236, 0) 100%);
  letter-spacing: 4px;
}

.body-content-bottom {
  display: flex;
  width: 1836px;
  height: 720px;
  margin-left: 18px;

  .el-divider--horizontal {
    height: 1px;
    background: linear-gradient(270deg, rgba(255, 255, 255, 0) 0%, rgba(135, 135, 135, 0.5) 49.31%, rgba(255, 255, 255, 0) 100%);
    display: block;
    margin: 10px 0;
  }
}

.body-content-bottom-left {
  width: 350px;
  height: 720px;
  background: rgba(13, 79, 90, 1);

  .el-input {
    width: 320px;
    height: 48px;
    margin-top: 20px;
    left: 50%;
    transform: translateX(-50%);
  }

  .el-icon-search {
    margin-right: 10px;
    font-size: 20px;
    height: 48px;
    color: rgba(0, 255, 255, 1);
    display: flex;
    align-items: center;
  }

  .el-icon-search:hover {
    cursor: pointer;
  }

  .el-input__inner {
    font-size: 16px;
    font-weight: 500;
    color: rgba(255, 255, 255, 1);
    width: 100%;
    height: 100%;
    background: rgba(80, 211, 225, 0.15);
    border: 2px solid rgba(15, 217, 180, 1);
    box-shadow: inset 3px 0px 0px rgba(80, 211, 225, 0.25);
    caret-color: rgba(255, 255, 255, 1);
  }

  .filter-tree {
    width: 320px;
    height: 590px;
    background: rgba(13, 79, 90, 1);
    color: rgba(255, 255, 255, 1);
    font-size: 16px;
    font-weight: 500;
    left: 50%;
    transform: translateX(-50%);
    overflow-y: auto;
    -ms-overflow-style: none;
    scrollbar-width: none;
  }

  .filter-tree::-webkit-scrollbar {
    display: none;
  }

  .filter-checkbox {
    margin-top: 15px;
    margin-left: 40px;
    background: rgba(13, 79, 90, 1);
    color: rgba(255, 255, 255, 1);
    font-size: 16px;
    font-weight: 500;
  }

  .el-tree-node {
    border-bottom: 1px solid;
    border-image: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(135, 135, 135, 0.5), rgba(255, 255, 255, 0)) 1 10;
  }

  .el-tree-node__label {
    font-size: 16px;
    font-weight: 500;
  }

  .el-tree .el-tree-node__content {
    height: 46px;
    background-color: rgba(13, 79, 90, 1);
  }

  .el-tree .el-tree-node__content:hover {
    background: rgba(189, 189, 189, 0.15);
    box-shadow: inset 0px 0px 4px rgba(80, 211, 225, 0.25);
  }

  .el-checkbox .el-checkbox__inner {
    border-radius: 50%;
    background-color: rgba(13, 79, 90, 1);
    border: 1px solid rgba(229, 230, 235, 1);
  }

  .el-checkbox .el-checkbox__input.is-checked .el-checkbox__inner {
    background: rgba(0, 125, 125, 1);
    border: 1px solid rgba(0, 125, 125, 1);
  }

  .el-checkbox__label {
    font-size: 16px;
    font-weight: 500;
    color: rgba(255, 255, 255, 1);
  }
}

.body-content-bottom-middle {
  width: 1000px;
  height: 720px;
  margin-left: 21px;
  margin-right: 10px;
  background: rgba(13, 79, 90, 1);


  .body-content-bottom-middle-table {
    display: flex;
    margin-top: 17px;
    margin-left: 15px;
    height: 634px;

    .el-table {
      background: rgba(13, 79, 90, 1);
    }

    .el-table .el-table__row {
      height: 40px;
    }

    .el-table th {
      text-align:center;
      font-size: 14px;
      font-weight: 500;
      color: rgba(229, 229, 229, 1);
      background: rgba(13, 79, 90, 1);
      border: none;
    }

    .el-table td {
      text-align:center;
      font-size: 14px;
      font-weight: 500;
      color: rgba(229, 229, 229, 1);
      background: rgba(13, 79, 90, 1);
      border: none;
    }

    .el-table::before {
      background: none;
    }

    .el-button {
      font-size: 14px;
      font-weight: 500;
      color: rgba(229, 229, 229, 1);
    }

    .el-button:hover {
      cursor: pointer;
    }

    .el-table__fixed::before,
    .el-table__fixed-right::before {
      background: none;
    }

    .el-table__body tr:hover>td {
      background: rgba(52, 59, 69, 1);
    }

    .el-table--medium th,
    .el-table--medium td {
      padding: 4px 0;
    }
  }

  .body-content-bottom-middle-page {
    height: 48px;
    display: flex;
    justify-content: center;

    .el-pagination__total {
      width: 60px;
      height: 28px;
      background: rgba(13, 79, 90, 1);
      color: rgba(193, 206, 222, 1);
      font-size: 12px;
      font-weight: 400;
      border: 1px solid rgba(177, 200, 230, 0.2);
      text-align: center;
    }

    .el-pagination .btn-prev {
      width: 24px;
      height: 28px;
      background: rgba(13, 79, 90, 1);
      color: rgba(193, 206, 222, 1);
      font-size: 12px;
      font-weight: 400;
      border: 1px solid rgba(177, 200, 230, 0.2);
    }

    .el-pagination .btn-prev:hover {
      background: rgba(30, 119, 135, 1);
      color: rgba(255, 255, 255, 1);
    }

    .el-pagination .btn-quickprev {
      width: 24px;
      height: 28px;
      background: rgba(13, 79, 90, 1);
      color: rgba(193, 206, 222, 1);
      font-size: 12px;
      font-weight: 400;
      border: 1px solid rgba(177, 200, 230, 0.2);
    }

    .el-pagination .btn-quickprev:hover {
      background: rgba(30, 119, 135, 1);
      color: rgba(255, 255, 255, 1);
    }

    .el-pagination .btn-next {
      width: 24px;
      height: 28px;
      background: rgba(13, 79, 90, 1);
      color: rgba(193, 206, 222, 1);
      font-size: 12px;
      font-weight: 400;
      border: 1px solid rgba(177, 200, 230, 0.2);
    }


    .el-pagination .btn-next:hover {
      background: rgba(30, 119, 135, 1);
      color: rgba(255, 255, 255, 1);
    }

    .el-pagination .btn-quicknext {
      width: 24px;
      height: 28px;
      background: rgba(13, 79, 90, 1);
      color: rgba(193, 206, 222, 1);
      font-size: 12px;
      font-weight: 400;
      border: 1px solid rgba(177, 200, 230, 0.2);
    }

    .el-pagination .btn-quicknext:hover {
      background: rgba(30, 119, 135, 1);
      color: rgba(255, 255, 255, 1);
    }


    .el-pagination .number {
      width: 24px;
      height: 28px;
      background: rgba(13, 79, 90, 1);
      color: rgba(193, 206, 222, 1);
      font-size: 12px;
      font-weight: 400;
      border: 1px solid rgba(177, 200, 230, 0.2);
    }

    .el-pagination .number:hover {
      background: rgba(30, 119, 135, 1);
      color: rgba(255, 255, 255, 1);
    }

    .el-pagination .number.active {
      background: rgba(30, 119, 135, 1);
      color: rgba(255, 255, 255, 1);
    }

    .el-pagination .el-icon-more {
      width: 24px;
      height: 28px;
      background: rgba(13, 79, 90, 1);
      color: rgba(193, 206, 222, 1);
      font-size: 12px;
      font-weight: 400;
      border: 1px solid rgba(177, 200, 230, 0.2);
    }

    .el-pagination .el-icon-more:hover {
      background: rgba(30, 119, 135, 1);
      color: rgba(255, 255, 255, 1);
    }
  }

}

.body-content-bottom-right {
  width: 455px;
  height: 720px;
  background: rgba(13, 79, 90, 1);

  .body-content-bottom-right-info {
    width: 100%;
    height: 270px;

    .info {

      margin-left: 12px;

      .el-input__prefix {
        width: 80px;
        height: 36px;
        display: flex;
        align-items: center;
        margin-left: 10px;
        font-size: 14px;
        font-weight: 500;
        color: rgba(229, 229, 229, 1);
        border: none;
        margin-top: 15px;
      }

      .el-input__inner {
        padding-left: 100px;
        padding-bottom: 1px;
        width: 420px;
        height: 36px;
        font-size: 14px;
        font-weight: 500;
        letter-spacing: 1px;
        color: rgba(229, 229, 229, 1);
        background: rgba(209, 209, 209, 0.15);
        box-shadow: inset 0px 0px 4px rgba(80, 211, 225, 0.25);
        border: none;
        margin-top: 15px;
      }

      .el-select__caret {
        height: 36px;
        margin-top: 15px;
      }

      .el-select-dropdown {
        background: rgba(7, 131, 140, 1);
        border: 1px solid rgba(7, 131, 140, 1);
      }

      .el-select-dropdown__item {
        font-size: 14px;
        font-weight: 500;
        letter-spacing: 1px;
        background: rgba(7, 131, 140, 1);
        color: rgba(255, 255, 255, 1);
      }

      .el-select-dropdown__item:hover {
        background: rgba(17, 173, 240, 0.1);
        border: 1px solid rgba(17, 173, 240, 0.2);
        box-shadow: inset 0px 0px 9.34px rgba(0, 153, 255, 0.5);
      }

      .el-select-dropdown__item.selected {
        color: #409EFF;
      }
    }

    .end-info {
      font-size: 14px;
      font-weight: 500;
      color: rgba(229, 229, 229, 1);
      margin-top: 15px;
      margin-left: 28px;

      .el-radio__label {
        font-size: 14px;
        font-weight: 500;
        color: rgba(255, 255, 255, 1);
      }

      .el-radio__inner {
        background: rgba(13, 79, 90, 1);
        border: 1px solid rgba(229, 230, 235, 1);
      }

      .el-radio .el-radio__input.is-checked .el-radio__inner {
        background: rgba(7, 131, 140, 1);
        border: 1px solid rgba(7, 131, 140, 1);
      }
    }
  }
}

.footer {
  width: 100%;
  height: 70px;
  display: flex;
  justify-content: center;
  align-items: center;

  .el-button--default {
    border-radius: 2px;
    background: rgba(80, 211, 225, 0.5);
    box-shadow: inset 0px 0px 4px rgba(80, 211, 225, 0.25);
    border: none;
    font-size: 16px;
    font-weight: 500;
    color: rgba(255, 255, 255, 1);
  }

  .el-button--primary {
    border-radius: 2px;
    background: linear-gradient(180deg, rgba(52, 213, 235, 1) 0%, rgba(12, 150, 168, 1) 100%);
    border: none;
    font-size: 16px;
    font-weight: 500;
    color: rgba(255, 255, 255, 1);
  }
}
</style>
